<template>
  <div class="admin-layout">
    <el-container class="layout-container">
      <!-- 侧边栏导航 -->
      <el-aside width="200px" class="sidebar">
        <div class="logo">
          <h2>管理系统</h2>
        </div>
        <el-menu
          :default-active="activeMenu"
          class="sidebar-menu"
          background-color="#545c64"
          text-color="#fff"
          active-text-color="#ffd04b"
          router
        >
          <el-menu-item index="/layout" @click="handleMenuClick('/layout')">
            <el-icon><House /></el-icon>
            <span>首页</span>
          </el-menu-item>
          
          <el-sub-menu index="user-management" @click="handleSubMenuClick('user-management')">
            <template #title>
              <el-icon><User /></el-icon>
              <span>用户管理</span>
            </template>
            <el-menu-item index="/layout/user" @="handleMenuClick('/layout/user')">用户列表</el-menu-item>
            <el-menu-item index="/users/roles" @click="handleMenuClick('/users/roles')">角色管理</el-menu-item>
            <el-menu-item index="/users/permissions" @click="handleMenuClick('/users/permissions')">权限管理</el-menu-item>
          </el-sub-menu>
          
          <el-sub-menu index="product-management" @click="handleSubMenuClick('product-management')">
            <template #title>
              <el-icon><Goods /></el-icon>
              <span>商品管理</span>
            </template>
            <el-menu-item index="/products/list" @click="handleMenuClick('/products/list')">商品列表</el-menu-item>
            <el-menu-item index="/products/categories" @click="handleMenuClick('/products/categories')">分类管理</el-menu-item>
            <el-menu-item index="/products/brands" @click="handleMenuClick('/products/brands')">品牌管理</el-menu-item>
          </el-sub-menu>
          
          <el-menu-item index="/orders" @click="handleMenuClick('/orders')">
            <el-icon><Document /></el-icon>
            <span>订单管理</span>
          </el-menu-item>
          
          <el-sub-menu index="system-settings" @click="handleSubMenuClick('system-settings')">
            <template #title>
              <el-icon><Setting /></el-icon>
              <span>系统设置</span>
            </template>
            <el-menu-item index="/settings/general" @click="handleMenuClick('/settings/general')">基本设置</el-menu-item>
            <el-menu-item index="/settings/security" @click="handleMenuClick('/settings/security')">安全设置</el-menu-item>
            <el-menu-item index="/settings/logs" @click="handleMenuClick('/settings/logs')">日志管理</el-menu-item>
          </el-sub-menu>
        </el-menu>
      </el-aside>
      
      <!-- 主体区域 -->
      <el-container>
        <!-- 头部 -->
        <el-header class="header">
          <div class="header-content">
            <h3>后台管理系统</h3>
            <div class="user-info">
              <el-dropdown @command="handleUserCommand">
                <span class="el-dropdown-link">
                  管理员 <el-icon ><ArrowDown /></el-icon>
                </span>
                <template #dropdown>
                  <el-dropdown-menu>
                    <el-dropdown-item command="profile">个人中心</el-dropdown-item>
                    <el-dropdown-item command="settings">账户设置</el-dropdown-item>
                    <el-dropdown-item command="logout" divided>退出登录</el-dropdown-item>
                  </el-dropdown-menu>
                </template>
              </el-dropdown>
            </div>
          </div>
        </el-header>
        
        <!-- 主内容区 -->
        <el-main class="main-content">
          <router-view />
        </el-main>
        
        <!-- 底部 -->
        <el-footer class="footer">
          © 2023 管理系统 - 后台管理平台
        </el-footer>
      </el-container>
    </el-container>
  </div>
</template>

<script setup>
import { ref, computed, h } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { 
  House, 
  User, 
  Goods, 
  Document, 
  Setting, 
  ArrowDown 
} from '@element-plus/icons-vue'

const route = useRoute()
const router = useRouter()

// 计算当前激活的菜单项
const activeMenu = computed(() => {
  const { meta, path } = route
  if (meta.activeMenu) {
    return meta.activeMenu
  }
  return path
})

// 菜单点击事件
const handleMenuClick = (path) => {
  console.log('菜单项被点击:', path)
  // 可以在这里添加额外的处理逻辑
}

// 子菜单点击事件
const handleSubMenuClick = (index) => {
  console.log('子菜单被点击:', index)
  // 可以在这里添加额外的处理逻辑
}

// 用户下拉菜单命令处理
const handleUserCommand = (command) => {
  console.log('用户命令:', command)
  switch (command) {
    case 'profile':
      router.push('/profile')
      break
    case 'settings':
      router.push('/account-settings')
      break
    case 'logout':
      handleLogout()
      break
    default:
      break
  }
}

// 用户退出登录功能
const handleLogout = () => {
  // 实现退出登录逻辑
  console.log('退出登录')
  // 示例: 清除用户信息并跳转到登录页
  // localStorage.removeItem('token')
  // router.push('/login')
}
</script>

<style scoped>
.admin-layout {
  height: 100vh;
}

.layout-container {
  height: 100%;
}

.sidebar {
  background-color: #545c64;
  color: white;
  overflow: hidden;
}

.logo {
  height: 60px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-bottom: 1px solid #6b727b;
}

.sidebar-menu {
  border: none;
  height: calc(100% - 60px);
}

.sidebar-menu :deep(.el-sub-menu__title:hover) {
  background-color: #434b54 !important;
}

.header {
  background-color: #fff;
  color: #333;
  box-shadow: 0 1px 4px rgba(0, 21, 41, 0.08);
  padding: 0;
}

.header-content {
  display: flex;
  justify-content: space-between;
  align-items: center;
  height: 100%;
  padding: 0 20px;
}

.user-info {
  cursor: pointer;
}

.main-content {
  background-color: #f0f2f5;
  padding: 20px;
  overflow: auto;
}

.footer {
  background-color: #fff;
  color: #666;
  text-align: center;
  height: 60px;
  line-height: 60px;
}
</style>